#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
APP_INSTALL_NAME="framepack"
APP_TARGET_DIR_NAME="framepack"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo 'App Dir: '${CURRENT_APP_PATH}

source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/env"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/docker_image_util.sh"

gen_image_repo_url $_DOCKER_IMAGE_NAME_LMD $_DOCKER_APP_VERSION
APP_IMAGE_REPO=$APP_IMAGE_REPO_RESULT

cd "$CURRENT_APP_PATH"

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi
if [ -n "$HF_ENDPOINT" ]; then
   export HF_ENDPOINT_PARAM="-e HF_ENDPOINT=$HF_ENDPOINT"
fi

CONTAINER_SERVER_PORT=7860
if [ -n "$GRADIO_SERVER_PORT" ]; then
  CONTAINER_SERVER_PORT=$GRADIO_SERVER_PORT
fi

if current_os_is_windows; then
   export MSYS_NO_PATHCONV=1 
fi

CONTAINER_NAME="framepack-lmdown-temp"
docker stop $CONTAINER_NAME || true
docker rm $CONTAINER_NAME || true

# run background
# -d

# If you want to close the app after terminal closed.
# --rm -it --init

docker run --rm -it --init --gpus all --name $CONTAINER_NAME \
  -v "${CURRENT_APP_PATH}/hf_download:/root/FramePack/hf_download" \
  -p $CONTAINER_SERVER_PORT:7860 $HF_ENDPOINT_PARAM \
  "$APP_IMAGE_REPO"
exit_code=$?

if [ $exit_code -ne 0 ]; then
   echo $exit_code
   # echo "lmdevent:docker_run_error"
   echo "docker_run_error"
   exit 1
fi

